package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.service.CacheService;
import com.iwdnb.bmnf.common.utils.BooleanUtils;
import com.iwdnb.gkgz.common.model.dto.StockLimitUpData;
import com.iwdnb.gkgz.common.utils.WorkDayUtils;
import com.iwdnb.gkgz.infrastructure.dao.model.query.StockLimitUpQuery;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockLimitUpRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.CacheConstants.LIMIT_UP_SYNC_CACHE_PREFIX;
import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_LIMIT_UP;

@Component
public class StockLimitUpRepositoryImpl implements StockLimitUpRepository {
    @Autowired
    private MetaDataDAO metaDataDAO;
    @Autowired
    private CacheService cacheService;

    @Override
    public List<StockLimitUpData> queryLimitUpList(StockLimitUpQuery query) {
        MetaQuery metaQuery = MetaQuery.init(query);
        List<StockLimitUpData> stockLimitUpDataList = metaDataDAO.queryListByCondition(OBJECT_CODE_STOCK_LIMIT_UP,
            metaQuery, StockLimitUpData.class);
        if (CollectionUtils.isEmpty(stockLimitUpDataList)) {
            return new ArrayList<>();
        }
        return stockLimitUpDataList;
    }

    @Override
    public void saveStockLimitUpData(StockLimitUpData data) {
        if (Objects.isNull(data.getId())) {
            metaDataDAO.insert(OBJECT_CODE_STOCK_LIMIT_UP, data);
        } else {
            metaDataDAO.update(OBJECT_CODE_STOCK_LIMIT_UP, data);
        }
    }

    @Override
    public void batchSaveStockLimitUpData(List<StockLimitUpData> dataList) {
        for (StockLimitUpData data : dataList) {
            saveStockLimitUpData(data);
        }
    }

    @Override
    public List<StockLimitUpData> queryNeedSyncLimitUpList(Date date) {
        //String cacheKey = LIMIT_UP_SYNC_CACHE_PREFIX + DateUtil.formatDate(date);
        //String content = cacheService.get(cacheKey);
        //if (StringUtils.isNotBlank(content)) {
        //    return JSON.parseArray(content, StockLimitUpData.class);
        //}
        date = DateUtil.beginOfDay(new Date());
        StockLimitUpQuery query = new StockLimitUpQuery();
        //查未卖出的股票信息
        query.setTldBuyTimeBegin(DateUtil.formatDateTime(WorkDayUtils.getBeforeWorkDay(date, 20)));
        query.setTldBuyTimeEnd(DateUtil.formatDateTime(DateUtil.endOfDay(date)));
        query.setTldFlag(BooleanUtils.TRUE);
        query.setTldBuyFlag(BooleanUtils.TRUE);
        query.setTldSellFlag(BooleanUtils.FALSE);
        List<StockLimitUpData> stockLimitUpDataList = queryLimitUpList(query);
        //查昨日卖出的股票
        query.setTldBuyTimeBegin(DateUtil.formatDateTime(WorkDayUtils.getBeforeWorkDay(date, 1)));
        query.setTldBuyTimeEnd(DateUtil.formatDateTime(DateUtil.endOfDay(date)));
        query.setTldFlag(BooleanUtils.TRUE);
        query.setTldBuyFlag(BooleanUtils.TRUE);
        query.setTldSellFlag(BooleanUtils.TRUE);
        List<StockLimitUpData> limitUpDataList = queryLimitUpList(query);
        if (CollectionUtils.isNotEmpty(limitUpDataList)) {
            stockLimitUpDataList.addAll(limitUpDataList);
        }
        //查待验证的股票信息
        Date beforeDateBegin = DateUtil.beginOfDay(WorkDayUtils.getBeforeWorkDay(date, 3));
        Date beforeDateEnd = DateUtil.endOfDay(WorkDayUtils.getBeforeWorkDay(date, 2));
        query = new StockLimitUpQuery();
        query.setDateBegin(DateUtil.formatDateTime(beforeDateBegin));
        query.setDateEnd(DateUtil.formatDateTime(beforeDateEnd));
        query.setTldFlag(BooleanUtils.FALSE);
        limitUpDataList = queryLimitUpList(query);
        if (CollectionUtils.isNotEmpty(limitUpDataList)) {
            stockLimitUpDataList.addAll(limitUpDataList);
        }
        //if (CollectionUtils.isNotEmpty(stockLimitUpDataList)) {
        //    cacheService.put(cacheKey, JSON.toJSONString(stockLimitUpDataList), 60);
        //}
        return stockLimitUpDataList;
    }
}
